package com.TeamJigsaw.DanteTrail.Models;

import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class SaveFileHandler {

	private JPanel parent;
	private JFrame frame;
	public SaveFileHandler() {
		frame = new JFrame();
		parent = new JPanel();
		frame.add(parent);

	}

	public Party read() throws IOException {
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(parent);

		String fileName = null;
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			fileName = fc.getSelectedFile().getPath();
		}

		Scanner scanner = null;
		
		try {
			if (fileName != null)
				scanner = new Scanner(new FileInputStream(fileName));
			else
				return null;
		} 
		catch(IOException e) {
			JOptionPane.showMessageDialog(null, "The file could not be loaded. Please select a  valid file.");
			return null;
		}
		
		Party loadParty = new Party();

		try {
			//load the player
			Player loadPlayer = new Player(scanner.next());
			loadPlayer.setProfession(Integer.parseInt(scanner.next()));
			loadPlayer.setStatus(Integer.parseInt(scanner.next()));
			loadPlayer.setHealth(Integer.parseInt(scanner.next()));
			loadParty.addPlayer(loadPlayer);
			
			//load the party members
			PartyMember m1 = new PartyMember(scanner.next(), getImage("resources\\partymember1.gif"));
			m1.setStatus(Integer.parseInt(scanner.next()));
			m1.setHealth(Integer.parseInt(scanner.next()));
			PartyMember m2 = new PartyMember(scanner.next(), getImage("resources\\partymember2.gif"));
			m2.setStatus(Integer.parseInt(scanner.next()));
			m2.setHealth(Integer.parseInt(scanner.next()));
			PartyMember m3 = new PartyMember(scanner.next(), getImage("resources\\partymembe3.gif"));
			m3.setStatus(Integer.parseInt(scanner.next()));
			m3.setHealth(Integer.parseInt(scanner.next()));
			loadParty.add(m1);
			loadParty.add(m2);
			loadParty.add(m3);
			
			//load the party inventory
			loadParty.setClothes(Integer.parseInt(scanner.next()));
			loadParty.setFood(Integer.parseInt(scanner.next()));
			loadParty.setAmmo(Integer.parseInt(scanner.next()));
			loadParty.setWheels(Integer.parseInt(scanner.next()));
			loadParty.setAxels(Integer.parseInt(scanner.next()));
			loadParty.setTongues(Integer.parseInt(scanner.next()));
			
			//load the party information
			loadParty.setPace(Integer.parseInt(scanner.next()));
			loadParty.setRations(Integer.parseInt(scanner.next()));
			loadParty.setMoney(Integer.parseInt(scanner.next()));
			loadParty.setWeight(scanner.nextInt());
			//load the wagon
			Wagon loadWagon = new Wagon();
			int oxenNo = Integer.parseInt(scanner.next());
			for(int i = 0; i < oxenNo; i++)
				loadWagon.addOxen(new Oxen());
			loadParty.setWagon(loadWagon);
			
			//load the trail progress
			loadParty.setTotalDistance(Integer.parseInt(scanner.next()));
			loadParty.setDistanceLeft(Integer.parseInt(scanner.next()));
		}
		catch(Exception e) {
			JOptionPane.showMessageDialog(null, "The save file could not be read. Corrupt save file?");
			return null;
		}
		finally {
			scanner.close();
		}
		
		return loadParty;
	}

	/** Write fixed content to the given file. */
	public void save(Party party) throws IOException  
	{
		String writeText = "";
		JFileChooser fc = new JFileChooser();

		String delimiter = " ";
		
		Player partyPlayer = party.getPlayer();
		//save the player
		writeText += partyPlayer.getName() + delimiter;
		writeText += partyPlayer.getProfession().getID() + delimiter;
		writeText += partyPlayer.getStatus() + delimiter;
		writeText += partyPlayer.getHealth() + delimiter;
		//save the party members
		for (PartyMember cur : party.getParty()) {
			writeText += cur.getName() + delimiter;
			writeText += cur.getStatus() + delimiter;
			writeText += cur.getHealth() + delimiter;
		}
		//save the party inventory
		writeText += party.getClothes() + delimiter;
		writeText += party.getFood() + delimiter;
		writeText += party.getAmmo() + delimiter;
		writeText += party.getWheels() + delimiter;
		writeText += party.getAxels() + delimiter;
		writeText += party.getTongue() + delimiter;
		//save the party information
		writeText += party.getPace() + delimiter;
		writeText += party.getRations() + delimiter;
		writeText += party.getMoney() + delimiter;
		writeText += party.getWeight() + delimiter;
		writeText += party.getWagon().getOxenNo() + delimiter;
		//save the trail progress
		writeText += party.getTotalDistance() + delimiter;
		writeText += party.getDistanceLeft() + delimiter;

		int returnVal = fc.showSaveDialog(parent);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String fileName = fc.getSelectedFile().getPath();

			Writer out = new OutputStreamWriter(new FileOutputStream(fileName + ".dat"));

			try {
				out.write(writeText);
			}
			finally {
				out.close();
			}
		}
	}

	/**
	 * Returns an Image based on the file name
	 * @param fileName The file name of the image
	 * @return The image of the file name
	 */
	public Image getImage(String fileName)
	{
		return (new ImageIcon(fileName).getImage());
	}
}
